/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sourcery;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import sourceryobjects.sRelationType;
import sourceryobjects.SceneGraph;
import sourceryobjects.sNode;
import sourceryobjects.sPoint;

/**
 *
 * @author claire
 */
public class MouseHandler implements MouseListener, MouseMotionListener {

    private sNode mouselocation;
    private sPoint mouseprevlocation;
    private sNode mouseselected;
    private SceneGraph scene;
    private sRelationType mouserelation;

//
//    public void setMouseRelation(sRelationType type) {
//        mouserelation = type;
//    }
    public MouseHandler() {
        mouselocation = new sNode(0, 0);
        mouseprevlocation = new sPoint(0, 0);
    }

    public MouseHandler(SceneGraph scene) {
        this();
        attachToScene(scene);
    }

    public void attachToScene(SceneGraph s) {
        scene = s;
        mouserelation = scene.getRelationType(0);
    }
    public double getX() { return mouselocation.getCoords().getX(); }
    public double getY() { return mouselocation.getCoords().getY(); }

    public void mouseClicked(MouseEvent e) {
    }

    public sNode findNode() {
        return scene.collide(mouselocation.getCoords());
    }
    public sNode getPoint() {
        return mouselocation;
    }

    public void mousePressed(MouseEvent e) {
        switch (e.getButton()) {
            case MouseEvent.BUTTON1:
                mouseselected = findNode();
                if (mouseselected != null) {
                    scene.addRelation(mouseselected, mouselocation, mouserelation);
                }
                break;
            default:
                break;
        }
    }

    public void mouseReleased(MouseEvent e) {
        if (mouseselected != null) {
            scene.removeRelation(mouselocation);
        }
        mouseselected = null;
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
        mouselocation.getCoords().set(new sPoint(e.getX(), e.getY()));
    }

    public void mouseMoved(MouseEvent e) {
        mouselocation.getCoords().set(new sPoint(e.getX(), e.getY()));
    }
}
